home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6071 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.crystalball.com!news
  2. From: Larry Weiss <lfw@oc.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help : Printf() format %E !
  5. Date: Thu, 22 Feb 1996 08:06:39 -0600
  6. Organization: crystalball.com
  7. Message-ID: <312C786F.6696@oc.com>
  8. References: <4g18nf$jps@ias2.ichange.com> <danpop.824495447@rscernix>
  9. NNTP-Posting-Host: external.oc.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14.  
  15. Dan Pop wrote:
  16.  > 
  17.  > In <4g18nf$jps@ias2.ichange.com> akmp@mindware.soft.net (anil kumar m p) writes:
  18.  > >i am involved converting programs written in Fortran onto C.
  19.  > >one problem that i am unable to solve is printing exponential
  20.  > >values in C.
  21.  > >Fortran prints exponential values with 0.4567E02
  22.  > >whereas C prints the same as 4.5670E01.
  23.  > >How do i make C print in 0.somethingEsomething format.
  24.  
  25.  
  26. > The %E format cannot do what you need, by definition.  So, you have to
  27. > split the number into mantissa and exponent and printf them separately.
  28. > You can do this either mathematically or by playing with stdio functions:
  29. >   double mantissa;
  30. >   int exponent;
  31. >   sprintf(buff, "%E", 0.4567E02);
  32. >   *strchr(buff, 'E') = ' ';
  33. >   sscanf(buff, "%lf %d", &mantissa, &exponent);
  34. >   mantissa /= 10;
  35. >   if (mantissa != 0) exponent++;
  36. >   else exponent = 0;
  37. >   printf("%fE%+.02d\n", mantissa, exponent);
  38. > If mathematical functions are inexpensive on your system, splitting
  39. > the number into mantissa and exponent with log10 and pow might be
  40. > more efficient. 
  41.  
  42. Or, use sprintf() to get a string containing the components, and
  43. then use character manipulations to adjust the formatting.
  44.